const mongoose = require("mongoose");
const { Send } = require("../utils/response");
const Wallpaper = require("../models/wallpaper");
const Collection = require("../models/collection");

class WallpaperController {
    static async list(req, res, next) {
        try {
            const list = await Wallpaper.find({}).populate(
                "collectionId",
                "name"
            );
            return Send(res, {
                message: "获取成功",
                data: list
            });
        } catch (error) {
            next(error);
        }
    }
    static async add(req, res, next) {
        try {
            const collection = await Collection.findById(req.body.collectionId);
            if (!collection) {
                return Send(res, {
                    code: -1,
                    message: "该图集不存在"
                });
            }
            const tags = req.body.tags
                ? [collection.name, ...req.body.tags.split("|")]
                : [collection.name];
            const wallpaper = await Wallpaper.create({
                name: req.body.name,
                thumbnail: req.body.thumbnail,
                imageUrl: req.body.imageUrl,
                collectionId: req.body.collectionId,
                summary: req.body.summary,
                tags
            });
            if (!wallpaper) {
                return Send(res, {
                    code: -1,
                    message: "壁纸添加失败"
                });
            }
            Send(res, {
                message: "壁纸添加成功",
                data: {
                    id: collection._id
                }
            });
        } catch (error) {
            next(error);
        }
    }
    static async detail(req, res, next) {
        try {
            const wallpaper = await Wallpaper.findOne({
                _id: req.params.id
            })
                .select("+createdAt +summary +imageUrl")
                .populate("collectionId", "name");
            if (!wallpaper) {
                return Send(res, {
                    code: -1,
                    message: "壁纸不存在"
                });
            }
            Send(res, {
                message: "获取壁纸详情成功",
                data: wallpaper
            });
        } catch (error) {
            next(error);
        }
    }
    static async delete(req, res, next) {
        try {
            const { deletedCount } = await Wallpaper.deleteOne({
                _id: req.params.id
            });
            if (deletedCount === 0) {
                return Send(res, {
                    code: -1,
                    message: "该壁纸不存在"
                });
            }
            return Send(res, {
                message: "壁纸删除成功",
                data: req.params.id
            });
        } catch (error) {
            next(error);
        }
    }
    static async update(req, res, next) {
        try {
            // 先拿出之前的标签
            const oldTags = await Wallpaper.findById(req.params.id).select(
                "tags"
            );
            if (!oldTags) {
                return Send(res, {
                    code: -1,
                    message: "该壁纸不存在"
                });
            }
            req.body.tags = [...oldTags.tags, ...req.body.tags.split("|")];
            const wallpaper = await Wallpaper.findByIdAndUpdate(
                req.params.id,
                req.body
            );
            Send(res, {
                message: "修改成功",
                data: wallpaper._id
            });
        } catch (error) {
            next(error);
        }
    }
    static async downloads(req, res, next) {
        try {
            const downloads = await Wallpaper.aggregate([
                {
                    $match: {
                        _id: new mongoose.Types.ObjectId(req.params.id)
                    }
                },
                {
                    $project: {
                        downloads: 1
                    }
                },
                {
                    $unwind: "$downloads"
                }
            ]);
            Send(res, {
                message: "下载列表获取成功",
                data: downloads
            });
        } catch (error) {
            next(error);
        }
    }
    static async ratings(req, res, next) {
        try {
            const ratings = await Wallpaper.aggregate([
                {
                    $match: {
                        _id: new mongoose.Types.ObjectId(req.params.id)
                    }
                },
                {
                    $project: {
                        ratings: 1
                    }
                },
                {
                    $unwind: "$ratings"
                }
            ]);
            Send(res, {
                message: "评分列表获取成功",
                data: ratings
            });
        } catch (error) {
            next(error);
        }
    }
}

module.exports = WallpaperController;
